<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Hoodoo::UUID</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        <h1>
            <span class="type">Class</span>
            Hoodoo::UUID
                <span class="parent">&lt;
                    Object
                </span>
        </h1>
        <ul class="files">
            <li><a href="../../files/lib/hoodoo/utilities/uuid_rb.html">lib/hoodoo/utilities/uuid.rb</a></li>
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
    <div class="description">
      
<p>Class that handles generation and validation of UUIDs. Whenever you want to
associate an identifier with something, you should use this class rather
than (e.g.) relying on identifiers generated by a database. This allows you
to cluster your database later on, should your application become big
enough, without having to worry about ID synchronisation across instances.</p>

    </div>








    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
        <dt>G</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-generate">generate</a>
              </li>
          </ul>
        </dd>
        <dt>V</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-valid-3F">valid?</a>
              </li>
          </ul>
        </dd>
    </dl>







      <!-- Section constants -->
      <div class="sectiontitle">Constants</div>
      <table border='0' cellpadding='5'>
          <tr valign='top' id='MATCH_16_PAIRS_OF_HEX_DIGITS'>
            <td class="attr-name">MATCH_16_PAIRS_OF_HEX_DIGITS</td>
            <td>=</td>
            <td class="attr-value"><pre>/^([[:xdigit:]]{2}){16}$/</pre></td>
          </tr>
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"><p>A regexp which, as its name suggests, only matches a string that contains
16 pairs of hex digits (with upper or lower case A-F). Legacy value kept in
case third party client code is using it.</p>

<p><a
href="http://stackoverflow.com/questions/287684/regular-expression-to-validate-hex-string">stackoverflow.com/questions/287684/regular-expression-to-validate-hex-string</a></p></td>
            </tr>
          <tr valign='top' id='MATCH_V4_UUID'>
            <td class="attr-name">MATCH_V4_UUID</td>
            <td>=</td>
            <td class="attr-value"><pre>/^[a-fA-F0-9]{12}4[a-fA-F0-9]{3}[89aAbB][a-fA-F0-9]{15}$/</pre></td>
          </tr>
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"><p>A regexp which matches V4 UUIDs with hyphens removed. Note that this is
more strict than <a
href="UUID.html#MATCH_16_PAIRS_OF_HEX_DIGITS">MATCH_16_PAIRS_OF_HEX_DIGITS</a>.</p></td>
            </tr>
      </table>



    <!-- Methods -->
      <div class="sectiontitle">Class Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-c-generate">
              <b>generate</b>()
            <a href="../../classes/Hoodoo/UUID.html#method-c-generate" name="method-c-generate" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Generate a unique identifier. Returns a 32 character string.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-generate_source')" id="l_method-c-generate_source">show</a>
              </p>
              <div id="method-c-generate_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/utilities/uuid.rb, line 29</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">generate</span>
  <span class="ruby-operator">::</span><span class="ruby-constant">SecureRandom</span>.<span class="ruby-identifier">uuid</span>().<span class="ruby-identifier">gsub!</span>( <span class="ruby-string">&#39;-&#39;</span>, <span class="ruby-string">&#39;&#39;</span> )
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-c-valid-3F">
              <b>valid?</b>( uuid )
            <a href="../../classes/Hoodoo/UUID.html#method-c-valid-3F" name="method-c-valid-3F" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Checks if a <a href="UUID.html">UUID</a> string is valid. Returns
<code>true</code> if so, else <code>false</code>.</p>
<dl class="rdoc-list note-list"><dt>uuid
<dd>
<p>Quantity to validate.</p>
</dd></dl>

<p>The method will only return <code>true</code> if the input parameter is a
String containing 32 mostly random hex digits representing a valid V4 <a
href="UUID.html">UUID</a> with hyphens removed.</p>

<p>Note that the validity of a <a href="UUID.html">UUID</a> says nothing about
where, if anywhere, it might have been used. So, just because a <a
href="UUID.html">UUID</a> is valid, doesn&#39;t mean you have (say) stored
something with that <a href="UUID.html">UUID</a> as the primary key in a
row in a database.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-valid-3F_source')" id="l_method-c-valid-3F_source">show</a>
              </p>
              <div id="method-c-valid-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/utilities/uuid.rb, line 46</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">valid?</span>( <span class="ruby-identifier">uuid</span> )
  <span class="ruby-identifier">uuid</span>.<span class="ruby-identifier">is_a?</span>( <span class="ruby-operator">::</span><span class="ruby-constant">String</span> ) <span class="ruby-operator">&amp;&amp;</span> ( <span class="ruby-identifier">uuid</span> <span class="ruby-operator">=~</span> <span class="ruby-constant">MATCH_V4_UUID</span> ) <span class="ruby-operator">!=</span> <span class="ruby-keyword">nil</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
</div>

    </div>
  </body>
</html>
